strlcpy and strlcat - Consistent, Safe, String Copy and Concatenation

نویسندگان

  • Todd C. Miller
  • Theo de Raadt
چکیده

As the prevalence of buffer overflow attacks has increased, more and more programmers are using size or length-bounded string functions such as strncpy() and strncat(). While this is certainly an encouraging trend, the standard C string functions generally used were not really designed for the task. This paper describes an alternate, intuitive, and consistent API designed with safe string copies in mind. There are several problems encountered when strncpy() and strncat() are used as safe versions of strcpy() and strcat(). Both functions deal with NUL-termination and the length parameter in different and non-intuitive ways that confuse even experienced programmers. They also provide no easy way to detect when truncation occurs. Finally, strncpy() zero-fills the remainder of the destination string, incurring a performance penalty. Of all these issues, the confusion caused by the length parameters and the related issue of NUL-termination are most important. When we audited the OpenBSD source tree for potential security holes we found rampant misuse of strncpy() and strncat(). While not all of these resulted in exploitable security holes, they made it clear that the rules for using strncpy() and strncat() in safe string operations are widely misunderstood. The proposed replacement functions, strlcpy() and strlcat(), address these problems by presenting an API designed for safe string copies (see Figure 1 for function prototypes). Both functions guarantee NUL-termination, take as a length parameter the size of the string in bytes, and provide an easy way to detect truncation. Neither function zero-fills unused bytes in the destination.

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

Copying and Concatenating C Strings with the str5 Functions

The copy and the concatenation of strings constitute a recurring subject of polemics within the C programmers community. They generally relate to the respective advantages and disadvantages of the three principal couples of functions which are strcpy()/strcat(), strncpy()/strncat() and strlcpy()/strlcat(). This article describes two new functions str5cpy() and str5cat() which were designed to r...

متن کامل

Design of a Safe String Library for C

For a variety of reasons, the C programming language is a favoured medium for writing system software. C takes a ‘lean language’ design approach. This involves the implementation of a small and consistent set of simple but powerful primitive features (data types, operators, control structures etc.) in the language while permitting, encouraging and empowering the user of the language to implemen...

متن کامل

Third-order Decentralized Safe Consensus Protocol for Inter-connected Heterogeneous Vehicular Platoons

In this paper, the stability analysis and control design of heterogeneous traffic flow is considered. It is assumed that the traffic flow consists of infinite number of cooperative non-identical vehicular platoons. Two different networks are investigated in stability analysis of heterogeneous traffic flow: 1) inter-platoon network which deals with the communication topology of lead vehicles and...

متن کامل

String Concatenation Optimization on Java Bytecode

String concatenation via the “+” operator is one of the most convenient things to do in Java, and also one of the most expensive, in terms of memory and performance. In this paper, we present a comprehensive approach to reduce the overhead of consuming string concatenation operations by implicitly transforming the Java bytecode. The transformation is based on the results of the liveness analysi...

متن کامل

Some Mathematical Aspects on Syntactic Discription

The purpose of this paper is to help linguists contruct a consistent, sufficient and less redundant syntax of language. An acceptable string corresponds to an expression or an utterance: it may be a natural text, a string of morphemes, a tree structure or any kind of representation. A sharp distinction is made between the syntactic function which is an attrib trin s and the distribution class w...

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

عنوان ژورنال:

دوره   شماره 

صفحات  -

تاریخ انتشار 1999